import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shenling_project/utils/eventBus.dart';
import 'package:shenling_project/utils/request.dart';
import 'package:shenling_project/utils/toast.dart';
import 'package:shenling_project/utils/tokenManager.dart';

class Loginpage extends StatefulWidget {
  const Loginpage({super.key});

  @override
  State<Loginpage> createState() => _LoginpageState();
}

class _LoginpageState extends State<Loginpage> {
  bool _isLookPassword = false;
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _usernameController.text = 'wg12345678';
    _passwordController.text = 'wuguang147';
  }

//登录
  _login() async {
    if (_usernameController.text.isEmpty) {
      return ToastUtil.showInfo('账号不能为空!');
    }
    if (_passwordController.text.isEmpty) {
      return ToastUtil.showInfo('密码不能为空!');
    }
    try {
      final res = await http.request(
          'post',
          '/driver/login/account',
          {
            'account': _usernameController.text,
            'password': _passwordController.text,
          },
          null);
      print(res);
      if (res['code'] != 200) return ToastUtil.showError('登录失败!');
      TokenManager().saveToken(res['data']);
      ToastUtil.showSuccess('登录成功!');
      // ToastUtil.showInfo(res['data']);
      Navigator.popUntil(context, (route) {
        return route.settings.name == '/';
      });
      event.fire(LoginGetData());
      event.fire(GetHomeData());
    } catch (e) {
      ToastUtil.showError('登录失败!,请稍后重试!');
    }
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
        canPop: false,
        child: Scaffold(
          // 设置导航条
          appBar: AppBar(
            title: const Text('神领物流'),
            backgroundColor: Colors.transparent,
            centerTitle: true,
            //左侧图标
            leading: IconButton(
              icon: const Icon(
                Icons.arrow_back_ios,
                color: Colors.transparent,
              ),
              onPressed: () {},
            ),
          ),
          // 设置主体内容
          body: Container(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                //第一行文案
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    const Text(
                      '账号登录',
                      style:
                          TextStyle(fontSize: 22, fontWeight: FontWeight.w700),
                    ),
                    Row(
                      children: [
                        const Text(
                          '手机号登录',
                          style: TextStyle(color: Colors.red, fontSize: 12),
                        ),
                        Image.asset(
                          'assets/images/right.png',
                          width: 14,
                          height: 14,
                          color: Colors.red,
                        )
                      ],
                    )
                  ],
                ),
                const SizedBox(
                  height: 20,
                ),
                //第二行输入框(账号)
                TextField(
                  controller: _usernameController,
                  decoration: const InputDecoration(hintText: '请输入账号'),
                ),
                const SizedBox(
                  height: 10,
                ),
                TextField(
                  controller: _passwordController,
                  keyboardType: TextInputType.visiblePassword,
                  obscureText: _isLookPassword,
                  decoration: InputDecoration(
                      hintText: '请输入密码',
                      suffixIcon: IconButton(
                        icon: Icon(_isLookPassword
                            ? Icons.visibility
                            : Icons.visibility_off),
                        onPressed: () {
                          setState(() {
                            _isLookPassword = !_isLookPassword;
                          });
                        },
                      )),
                ),

                const SizedBox(
                  height: 20,
                ),
                //登录按钮
                SizedBox(
                    width: MediaQuery.of(context).size.width,
                    child: ElevatedButton(
                      style:
                          ElevatedButton.styleFrom(backgroundColor: Colors.red),
                      onPressed: () {
                        _login();
                      },
                      child: const Text(
                        '登录',
                        style: TextStyle(color: Colors.white, fontSize: 20),
                      ),
                    ))
              ],
            ),
          ),
        ));
  }
}
